
package com.wwy.monitor.controller;

import com.wwy.monitor.common.PageResult;
import com.wwy.monitor.common.ResponseEnum;
import com.wwy.monitor.common.ServiceResponse;
import com.wwy.monitor.model.enumeration.DeleteEnum;
import com.wwy.monitor.model.tddl.dto.MonitorAppDTO;
import com.wwy.monitor.model.tddl.query.MonitorAppQuery;
import com.wwy.monitor.service.MonitorAppService;
import com.wwy.monitor.util.OneLogUtil;
import com.wwy.monitor.util.UserUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Date;

@RestController
@RequestMapping(value = "/api/")
public class MonitorAppController {
    @Resource(name = "monitorAppService")
    private MonitorAppService monitorAppService;

    @GetMapping("/monitorApps")
    @ResponseBody
    private ServiceResponse getAllMonitorApp(MonitorAppQuery monitorAppQuery) {
        monitorAppQuery.setIsDelete(DeleteEnum.NOT_DELETE.getValue());
        return ServiceResponse.success(monitorAppService.selectByQuery(monitorAppQuery));
    }

    /**
     * 分页获取
     *
     * @return
     */
    @GetMapping("monitorApp/page")
    @ResponseBody
    private ServiceResponse page(Integer pageNum, Integer pageSize, Long aoneAppId, String appName) {
        if (pageNum == null || pageSize == null) {
            return ServiceResponse.paramIllegal("分页参数不能为空");
        }
        MonitorAppQuery query = new MonitorAppQuery();
        if (aoneAppId != null) {
            query.setAoneAppId(aoneAppId);
        }
        if (StringUtils.isNotBlank(appName)) {
            query.setAppName(appName);
        }
        Integer offset = (pageNum - 1) * pageSize;
        query.setOffset(offset);
        query.setSize(pageSize);
        query.setIsDelete(DeleteEnum.NOT_DELETE.getValue());
        PageResult pageResult = new PageResult();
        pageResult.setCurrentPage(pageNum);
        pageResult.setPageSize(pageSize);
        pageResult.setTotalCount(monitorAppService.count(query));
        pageResult.setDatas(monitorAppService.selectByQuery(query));
        pageResult.getPages();
        return ServiceResponse.success(pageResult);
    }

    @PostMapping("monitorApp/add")
    @ResponseBody
    private ServiceResponse add(HttpServletRequest request, @RequestBody MonitorAppDTO dto)
            throws IOException, ServletException {
        OneLogUtil.getOpLog("monitorApp/add", "应用添加", request);
        if (dto == null || dto.getAoneAppId() == null
                || StringUtils.isBlank(dto.getAppName())) {
            return ServiceResponse.fail(ResponseEnum.PARAM_EMPTY.getCode(), ResponseEnum.PARAM_EMPTY.getDesc());
        }
        //同一应用只能添加一次
        MonitorAppQuery query = new MonitorAppQuery();
        query.setAppName(dto.getAppName());
        query.setIsDelete(DeleteEnum.NOT_DELETE.getValue());
        monitorAppService.checkApp(dto);
        String empId = UserUtils.getBucEmpId(request);
        dto.setCreateUser(empId);
        dto.setIsDelete(DeleteEnum.NOT_DELETE.getValue());
        boolean flag = monitorAppService.insert(dto);
        if (!flag) {
            return ServiceResponse.fail(ResponseEnum.SAVE_EXCEPTION.getCode(), ResponseEnum.SAVE_EXCEPTION.getDesc());
        }
        return ServiceResponse.success(ResponseEnum.SUCCESS.getDesc());
    }

    @PostMapping("monitorApp/update")
    @ResponseBody
    private ServiceResponse update(HttpServletRequest request, @RequestBody MonitorAppDTO dto)
            throws IOException, ServletException {
        OneLogUtil.getOpLog("monitorApp/update", "应用更新", request);
        if (dto == null || dto.getId() == null) {
            return ServiceResponse.fail(ResponseEnum.PARAM_EMPTY.getCode(), ResponseEnum.PARAM_EMPTY.getDesc());
        }
        String empId = UserUtils.getBucEmpId(request);
        dto.setUpdateUser(empId);
        monitorAppService.checkApp(dto);
        boolean flag = monitorAppService.updateById(dto);
        if (!flag) {
            return ServiceResponse.fail(ResponseEnum.UPDATE_EXCEPTION.getCode(), ResponseEnum.UPDATE_EXCEPTION.getDesc());
        }
        return ServiceResponse.success(ResponseEnum.SUCCESS.getDesc());
    }

    @PostMapping("monitorApp/updateStatus")
    @ResponseBody
    private ServiceResponse updateStatusById(HttpServletRequest request, @RequestBody MonitorAppDTO dto)
            throws IOException, ServletException {
        OneLogUtil.getOpLog("monitorApp/updateStatus", "应用更新状态", request);
        String empId = UserUtils.getBucEmpId(request);
        dto.setUpdateUser(empId);
        dto.setUpdateTime(new Date(System.currentTimeMillis()));
        boolean flag = monitorAppService.updateStatusById(dto);
        if (!flag) {
            return ServiceResponse.fail(ResponseEnum.UPDATE_EXCEPTION.getCode(), ResponseEnum.UPDATE_EXCEPTION.getDesc());
        }
        return ServiceResponse.success(ResponseEnum.SUCCESS.getDesc());
    }

    @PostMapping("monitorApp/delete")
    @ResponseBody
    private ServiceResponse delete(@RequestBody MonitorAppDTO dto) {
        Long id = dto.getId();
        if (id == null) {
            return ServiceResponse.fail(ResponseEnum.PARAM_EMPTY.getCode(), ResponseEnum.PARAM_EMPTY.getDesc());
        }
        boolean flag = monitorAppService.deleteById(id);
        if (!flag) {
            return ServiceResponse.fail(ResponseEnum.DELETE_EXCEPTION.getCode(), ResponseEnum.DELETE_EXCEPTION.getDesc());
        }
        return ServiceResponse.success(ResponseEnum.SUCCESS.getDesc());
    }
}